<?php
/**
 * @author baby&star
 *
 *
 */
class Search {
	//TODO - Insert your code here
	private $_id = "public";
	
	private $_url = NULL;
	
	private $_host = NULL;
	
	private $_webRoot = NULL;
	
	private $_hrefArr = Array();
	
	function __construct($url,$id) {

		//TODO - Insert your code here
		$this->_id  = $id;
		
		$this->_url = $url;
		
		$domain = parse_url($url);
		//print_r($domain);
		
		$this->_host = $domain["scheme"]."://".$domain["host"];
		
		
		$path = explode('/',$domain['path']);
		
		array_pop($path);
		
		$this->_webRoot = $this->_host.implode('/',$path);
		
		$this->_hrefArr[] = array("url"=>$this->_url,"isopen" => false);
		
	}
	function start(){
		//$this->getUrlContent();
		foreach ($this->_hrefArr as $k=>$v){
			//print_r($v);
			if($v["isopen"]==false){
				
				$this->_url = $v["url"];
				
				$this->getUrlContent();
				
				$this->_hrefArr[$k]["isopen"] = true;
				
			}
		}
		
	}
	/**
	 * 打开url取页面内容
	 * @param $type get|post
	 */
	function getUrlContent($type="get"){
		
		if($type == "get"){
			
			$content = $this->getContent();
			
		}else{
			
			$content = $this->postContent();
			
		}
		
		//print_r($content);
		//exit;
		//提取url
		$this->getHref($content);
		$this->getImgs($content);
		
	}
	/**
	 * 获取页面URL地址
	 * 1，在本域下
	 * 2，链接不重复
	 * @param String $content
	 */
	private function getHref($content){
		
		preg_match_all("/<a.*?href=['\"]?(.*?)['\"].*?>/i",$content,$arr);
		
		if(empty($arr[1])){
			return false;
		}
		
		$arr = $arr[1];
		
		$hrefArr = "";
		
		foreach($arr as $k=>$v){
			$v = $this->getUrl($v);
			if(!empty($v)){
				$hrefArr[] = $v;
			}
		}
		
		$this->trimMore($hrefArr);
		
	}
	private function getUrl($url,$debug=false){
		
		if(empty($url)){
			return false;
		}
		
		
		$domain = parse_url($this->_url);
		
		$domain = $domain["host"];
		$domain = str_replace("www:","",$domain);
		$domain = str_replace(".","\.",$domain);
		//判断URL是否完整
		
		if(preg_match("/http:\/\//",$url)||preg_match("/https:\/\//",$url)){
			
			//判断是否在此域下
			if(!preg_match("/$domain/",$url) && $debug){
				$url = false;
			}
			
		}else{
			
			//补全完整成URL
			$str = substr($url,0,1);
			
			if($str == '/'){
				$url = $this->_host.$url;
				
			}else{
				$url = $this->_webRoot.$url;
				
			}
			
		}
		if($debug){
			echo $url;exit;
		}
		return $url;
	}
	/**
	 * 取图片并保存
	 */
	private function getImgs($content){
		//取所有的图片
		$imgContents = preg_match_all("/<img.+?src=[\"\']?([\d\D]+?)[\"\']?\s+.+?\/{0,1}>/i",$content,$imgRs);
		
		//print_r($imgRs);exit;
		
		if(!empty($imgRs[1])){
			
			foreach($imgRs[1] as $k=>$v){
				
				$v = $this->getUrl($v,false);
				
				if(!empty($v)){
					$this->downFile($v);
				}
				
			}
		}
		return true;
		preg_match_all("/<link.+?href=\"(.+\.css)\".+?\/>/i",$content,$cssRs);
		
		$cssRs = $cssRs[1];
		
		array_shift($cssRs);
		
		$domain = parse_url($this->_url);
		$domain = $domain["host"];
		$domain = str_replace("www:","",$domain);
		$domain = str_replace(".","\.",$domain);
		
		foreach($cssRs as $k=>$v){
			
			$str = substr($v,0,1);
			
			if(preg_match("/http:\/\//",$url)||preg_match("/https:\/\//",$v)){
				//判断是否在此域下
				if(preg_match("/".$domain."/",$url)){
					
					$this->downImgCss($v,$this->_host);
				}else{
					return false;
				}
			}else{
				if($str == "/"){
					$v = $this->_host.'/'.$v;
					
					$this->downImgCss($v,$this->_host);
					
				}else{
					$v = $this->_webRoot.'/'.$v;
					
					$this->downImgCss($v,$this->_webRoot);
				}
			}
			
		}
		
	}
	private function downImgCss($v,$host){
		
		$strstr = "/background-image:url\([\'\"]?(.+\.png)[\'\"]?\)[;]?/i";
		
		$content = $this->getContent($v);
				
		preg_match_all($strstr,$content,$cssImg);
		
		if(is_array($cssImg)){
			
			foreach($cssImg[1] as $k=>$v){
				
				$v = str_replace('../','',$v);
				
				$v = $host.'/'.$v;
				echo $v;exit;
				$this->downFile($v);
			}
		}
		
	}
	/**
	 * 去除重复的
	 * 
	 */
	private function trimMore($arr){
		$arr = array_unique($arr);
		foreach($arr as $key=>$val){
			$is = true;
			foreach($this->_hrefArr as $k=>$v){
				if($v["url"] == $val){
					unset($arr[$key]);
					$is = false;
					break;
				}
			}
			if($is == true){
				$addArr = array("url"=>$val,"isopen"=>false);
				$this->_hrefArr[] = $addArr;
			}
		}
		
	}
	private function getContent($url = NULL){
		
		if($url == NULL){
			$url = $this->_url;
		}
		
		$ch = curl_init(); 
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt($ch,CURLOPT_COOKIEFILE,$this->cookies);
        //curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookies);                
        curl_setopt($ch,CURLOPT_HEADER,0);        
        $str = curl_exec($ch);
        
        curl_close($ch);
        
        if(empty($str)){
        	
        	$str = file_get_contents($url);
        }
        
       
        return $str;
	}
	private function postContent(){
		$ch = curl_init();    
	    curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);  
	    curl_setopt($ch, CURLOPT_URL,$this->_url);     
	    curl_setopt($ch, CURLOPT_POST, 1 );     
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);     
	    $data = curl_exec($ch);        
	    curl_close($ch);      
	    if ($data){   
	        return $data;
	    }else{
	    	$url .= $data;
	    	$data = file_get_contents($this->_url);
	        if($data){
	        	return $data;
	        }else{
	        	return false;
	        }
	    }
	}
	/**
	 * 
	 * 下载并保存图片文件到相应的目录
	 * @param String $url
	 * @param void
	 */
	protected  function downFile($url,$debug=false){
		//检查文件是否存 在
		$dir = "upfiles/".$this->_id;
		if(!is_dir($dir)){
			mkdir($dir,777);
		}
		if(!preg_match("/\.jpg|\.gif|\.png/i",$url)){
			$name = time().".jpg";
			$name = $dir."/".$name;
		}else{
			$name = explode('/', $url);
			$name = end($name);
			$name = $dir."/".$name;
		}
		
		$fp   = @fopen($name, 'a');
		if($fp==false){
			return false;
		}
		
		$ch = curl_init(); 
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt($ch,CURLOPT_COOKIEFILE,$this->cookies);
        //curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookies);                
        curl_setopt($ch,CURLOPT_HEADER,0);        
        $img = curl_exec($ch);
        if(empty($img)){
        	
        	$img = file_get_contents($url);
        }
        
        curl_close($ch);
        
		fputs($fp, $img, strlen($img));
		fclose($fp);
		
		return $name;
	}
	/**
	 *
	 */
	function __destruct() {

		//TODO - Insert your code here
	}
}


?>